<template>
  <el-dialog
    :title="!dataForm.detailId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="字典标签" prop="label">
        <el-input v-model="dataForm.label" placeholder="字典标签"></el-input>
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input v-model="dataForm.value" placeholder="字典值"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="dictSort">
        <el-input-number
          :min="0"
          :max="999"
          v-model="dataForm.dictSort"
          placeholder="排序"
          style="width:100%"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        detailId: 0,
        dictId: "",
        label: "",
        value: "",
        dictSort: 999,
        createBy: "",
        updateBy: "",
        createTime: "",
        updateTime: ""
      },
      dataRule: {
        label: [
          { required: true, message: "字典标签不能为空", trigger: "blur" },
          {
            min: 1,
            max: 255,
            message: "最多输入255个字符"
          }
        ],
        value: [
          { required: true, message: "字典值不能为空", trigger: "blur" },
          {
            min: 1,
            max: 255,
            message: "最多输入255个字符"
          }
        ],
        dictSort: [{ required: true, message: "排序不能为空", trigger: "blur" }]
      }
    };
  },
  props: ['detailDictId'],
  methods: {
    init(id) {
      this.dataForm.detailId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.detailId) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dictdetail/info/${this.dataForm.detailId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dictId = data.sysDictDetail.dictId;
              this.dataForm.label = data.sysDictDetail.label;
              this.dataForm.value = data.sysDictDetail.value;
              this.dataForm.dictSort = data.sysDictDetail.dictSort;
              this.dataForm.createBy = data.sysDictDetail.createBy;
              this.dataForm.updateBy = data.sysDictDetail.updateBy;
              this.dataForm.createTime = data.sysDictDetail.createTime;
              this.dataForm.updateTime = data.sysDictDetail.updateTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      debugger
      this.dataForm.dictId = this.detailDictId;
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dictdetail/${
                !this.dataForm.detailId ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              detailId: this.dataForm.detailId || undefined,
              dictId: this.dataForm.dictId,
              label: this.dataForm.label,
              value: this.dataForm.value,
              dictSort: this.dataForm.dictSort,
              createBy: this.dataForm.createBy,
              updateBy: this.dataForm.updateBy,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
